package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxSubFoodDao;
import com.syni.mdd.yhd.common.all.entity.BmsDxSubFood;
import com.syni.mdd.yhd.common.all.service.BmsDxSubFoodService;
import com.syni.mdd.yhd.common.all.service.BmsUserBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxSubFoodServiceImpl extends BaseService<BmsDxSubFood, Integer> implements BmsDxSubFoodService {

    @Autowired
    BmsDxSubFoodDao bmsDxSubFoodDao;

    @Autowired
    BmsUserBusinessService bmsUserBusinessService;

    @Override
    public BmsDxSubFoodDao getBaseDao() {
        return bmsDxSubFoodDao;
    }


    public List<BmsDxSubFood> getSubFoodByLibraryId(Integer libraryId) {
        return bmsDxSubFoodDao.findByBmsLibraryId(libraryId);
    }

    @Override
    public Map<Integer,BmsDxSubFood> getSubFoodByIds(Set<Integer> libraryIds, boolean isDisplayDelete) {
        Map<Integer,BmsDxSubFood> bmsDxSubFoodMap = new HashMap<>();
        List<BmsDxSubFood> bmsDxSubFoods;
        if(isDisplayDelete){
            bmsDxSubFoods = bmsDxSubFoodDao.findByIdIn(libraryIds);
        }else {
            bmsDxSubFoods = bmsDxSubFoodDao.findByIdInAndIsDelete(libraryIds,Constants.COMMON_FLAG_FALSE_INT);
        }
        if(bmsDxSubFoods != null && bmsDxSubFoods.size() > 0){
            for(BmsDxSubFood bmsDxSubFood : bmsDxSubFoods){
                bmsDxSubFoodMap.put(bmsDxSubFood.getId(),bmsDxSubFood);
            }
        }
        return bmsDxSubFoodMap;
    }

    @Override
    public BmsDxSubFood getSubFoodByIds(Integer subId) {
        return bmsDxSubFoodDao.findByIdAndIsDelete(subId,Constants.COMMON_FLAG_FALSE_INT);
    }

    public Map<Integer, List<BmsDxSubFood>> getSubFoodByType(Set<Integer> librayIds) {
        Map<Integer,List<BmsDxSubFood>> bFoodsMap = new HashMap<>();
        Sort sort = Sort.by(Sort.Direction.ASC,"newTime");
        List<BmsDxSubFood> bmsDxSubFoods = bmsDxSubFoodDao.findByBmsLibraryIdInAndIsDelete(librayIds,Constants.COMMON_FLAG_FALSE_INT,sort);
        if(bmsDxSubFoods != null && bmsDxSubFoods.size() > 0){
            for(BmsDxSubFood bmsDxSubFood : bmsDxSubFoods){
                if(!bFoodsMap.containsKey(bmsDxSubFood.getBmsLibraryId())){
                    bFoodsMap.put(bmsDxSubFood.getBmsLibraryId(),new LinkedList<>());
                }
                bFoodsMap.get(bmsDxSubFood.getBmsLibraryId()).add(bmsDxSubFood);
            }
        }
        return bFoodsMap;
    }
}
